<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card v-loading="loading" class="tree-card">
        <el-tabs v-model="activeName" @tab-click="handleClick">
          <!-- 左侧 -->
          <el-tab-pane label="角色管理" name="first">
            <!-- 按钮 -->
            <el-button
              icon="el-icon-plus"
              size="small"
              type="primary"
              @click="clickShowAdd"
            >
              新增角色
            </el-button>
            <!-- 表格 -->
            <el-table :data="tableData" style="width: 100%">
              <el-table-column
                type="index"
                :index="indexfn"
                label="序号"
                width="180"
              >
              </el-table-column>
              <el-table-column prop="name" label="名称" width="180">
              </el-table-column>
              <el-table-column prop="description" label="描述">
              </el-table-column>
              <el-table-column label="操作">
                <template #default="{ row }">
                  <el-button
                    size="small"
                    type="success"
                    @click="displanrolefn(row.id)"
                  >
                    分配权限
                  </el-button>
                  <el-button
                    @click="editpoledetalfn(row.id)"
                    size="small"
                    type="primary"
                    >编辑</el-button
                  >
                  <el-button
                    size="small"
                    type="danger"
                    @click="delrolefn(row.id)"
                  >
                    删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
            <!--
              @current-change // 改变当前页执行
               @size-change // 每页条数变化，切换每页条数
                :page-sizes 可供选择的每页条数
                layout 布局控件
                :current-page 当前页数
                total 总条目数-->
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="currentPage"
              :page-sizes="[3, 5, 7, 10, 50]"
              :page-size="pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
            >
            </el-pagination>
          </el-tab-pane>

          <el-tab-pane label="公司信息">
            <!-- 警告信息 -->
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <!-- 表单 -->
            <el-form label-width="120px" style="margin-top: 50px">
              <el-form-item label="公司名称">
                <el-input
                  v-model="companyform.name"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  v-model="companyform.companyAddress"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  v-model="companyform.mailbox"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  type="textarea"
                  :rows="3"
                  disabled
                  v-model="companyform.remarks"
                  style="width: 400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <el-dialog :title="showtitle" :visible="showDialog" @close="closeDialog">
        <el-form
          ref="roleForm"
          :model="form"
          :rules="rules"
          label-width="100px"
        >
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入角色名称" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="form.description" placeholder="请输入角色描述" />
          </el-form-item>
        </el-form>

        <template #footer>
          <el-button @click="closeDialog">取消</el-button>
          <el-button type="primary" @click="submitrolefn">确认</el-button>
        </template>
      </el-dialog>
      <!-- 分配权限的弹层 -->
      <el-dialog
        title="分配权限"
        :visible="showAssignDialog"
        @close="closeAssignDialog"
      >
        <template #footer>
          <el-tree
            ref="tree"
            :data="rolelist"
            :props="{ label: 'name' }"
            show-checkbox
            :check-strictly="true"
            :default-checked-keys="permids"
            node-key="id"
          ></el-tree>
          <div style="text-align: right">
            <el-button @click="closeAssignDialog">取消</el-button>
            <el-button type="primary" @click="submitfn">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  rolelistaopi,
  addroleaopi,
  delroleaopi,
  getroledetaapi,
  editroledetaapi
} from '@/api/setting'
import { getcompanyapi } from '@/api/company'
import { reqGetPermissionList } from '@/api/permsing'
import { translisttreedata } from '@/utils/index'
import { fnepeiroledetaapi } from '@/api/setting'
export default {
  data() {
    return {
      loading: false,
      activeName: 'first',
      tableData: [],
      currentPage: 1,
      total: 0,
      page: 1, // 当前页码数
      pagesize: 50, // 每一页数据条数
      showDialog: false,
      showAssignDialog: false,
      rolelist: [],
      roleid: '',
      id: '',
      permids: [], // 角色具备的权限点
      form: {
        name: '',
        description: ''
      },
      companyform: {
        name: '',
        companyAddress: '',
        mailbox: '',
        remarks: ''
      },

      rules: {
        name: [
          {
            required: true,
            message: '请输入角色名称',
            trigger: ['blur', 'change']
          }
        ],
        description: [
          {
            required: true,
            message: '请输入角色描述',
            trigger: ['blur', 'change']
          }
        ]
      }
    }
  },
  async created() {
    this.getrolelistfn()
    const res = await getcompanyapi(this.$store.state.user.userinfo.companyId)
    console.log(res)
    this.companyform = res.data
  },
  computed: {
    showtitle() {
      return this.form.id ? '编辑角色' : '添加角色'
    }
  },
  methods: {
    // 请求接口
    // handleNodeClick(data) {
    //   console.log(data)
    // },
    async getrolelistfn() {
      this.loading = true
      const { data } = await rolelistaopi(this.page, this.pagesize)
      this.tableData = data.rows
      this.total = data.total
      this.id = data.id
      this.loading = false
    },
    indexfn(index) {
      return index + 1 + (this.page - 1) * this.pagesize
    },
    handleClick(tab, event) {
      console.log(tab, event)
    },
    // 每页条数变化，切换每页条数
    handleSizeChange(val) {
      this.pagesize = val
      this.page = 1
      this.getrolelistfn()
      console.log(`每页 ${val} 条`)
    },
    // 改变当前页执行
    handleCurrentChange(val) {
      this.page = val
      this.getrolelistfn()
      console.log(`当前页: ${val}`)
    },
    // 关闭弹层
    closeDialog() {
      this.showDialog = false
      this.form = {}
      this.$refs.roleForm.resetFields()
    },
    // 打开弹层
    clickShowAdd() {
      this.showDialog = true
    },
    // 新增角色&编辑角色
    submitrolefn() {
      this.$refs.roleForm.validate(async (valid, val) => {
        if (!valid) return
        if (this.form.id) {
          // 编辑接口
          await editroledetaapi(this.form)
          this.$message.success('修改成功')
        } else {
          // 添加接口
          await addroleaopi(this.form)
          this.$message.success('添加成功')
        }
        this.closeDialog()
        this.getrolelistfn()
        this.form = {}
        this.$refs.roleForm.resetFields()
      })
    },
    // 删除角色
    delrolefn(id) {
      this.$confirm('此操作将永久删除该部门，是否继续', '温馨提示')
        .then(async () => {
          await delroleaopi(id)
          this.$message.success('删除成功')
          // 当前表格长度是否是1，是1，则页码--，当前页是不是在第一页，如果不是第一页就--
          if (this.tableData.length === 1 && this.page > 1) {
            this.page--
          }
          this.getrolelistfn()
        })
        .catch(() => {
          this.$message('取消成功')
        })
    },
    // 点击编辑按钮
    async editpoledetalfn(id) {
      this.clickShowAdd()
      const { data } = await getroledetaapi(id)
      this.form = data
    },
    // 分配权限
    async displanrolefn(id) {
      this.permids = []
      this.roleid = id
      this.showAssignDialog = true
      const res = await reqGetPermissionList()
      const resb = translisttreedata(res.data, '0')
      this.rolelist = resb
      // console.log(resb)
      const resc = await getroledetaapi(id)
      // console.log(resc.data.permIds)
      this.permids = resc.data.permIds
    },
    closeAssignDialog() {
      this.showAssignDialog = false
    },
    // 给角色分配权限
    async submitfn() {
      const obj = {
        id: this.roleid,
        permIds: this.$refs.tree.getCheckedKeys()
      }
      await fnepeiroledetaapi(obj)
      this.$message.success('分配成功')
      this.showAssignDialog = false
    }
  }
}
</script>

<style></style>
